﻿using CinemaMaestro.DataLayer.Contracts.Entities;
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CinemaMaestro.WebApplication.Models
{
    public class MovieViewModels
    {
        public MovieViewModels()
        {
            this.Categories = new HashSet<CategoryViewModels>();
            this.Actors = new HashSet<ActorViewModels>();
            this.Directors = new HashSet<DirectorViewModels>();
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public int Duration { get; set; }
        public string Description { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Image { get; set; }
        public ICollection<CategoryViewModels> Categories { get; set; }
        public ICollection<ActorViewModels> Actors { get; set; }
        public ICollection<DirectorViewModels> Directors { get; set; }

        public static implicit operator MovieViewModels(Movie movie)
        {
            if (movie != null)
            {
                return new MovieViewModels
                {
                    Id = movie.Id,
                    Title = movie.Title,
                    Duration = movie.Duration,
                    Description = movie.Description,
                    ReleaseDate = movie.ReleaseDate,
                    Image = movie.Image != null ? Convert.ToBase64String(movie.Image) : string.Empty,
                    Categories = ConvertToCategoryViewModelList(movie.Categories),
                    Actors = ConvertToActorViewModelList(movie.Actors),
                    Directors = ConvertToDirectorViewModelList(movie.Directors)
                };
            }
            return null;
        }

        public static implicit operator Movie(MovieViewModels movieVM)
        {
            return new Movie
            {
                Id = movieVM.Id,
                Title = movieVM.Title,
                Duration = movieVM.Duration,
                Description = movieVM.Description,
                ReleaseDate = movieVM.ReleaseDate,
                Image = Encoding.ASCII.GetBytes(movieVM.Image),
                Categories = ConvertToCategoryList(movieVM.Categories),
                Actors = ConvertToActorList(movieVM.Actors),
                Directors = ConvertToDirectorList(movieVM.Directors)
            };
        }

        private static List<DirectorViewModels> ConvertToDirectorViewModelList(ICollection<Director> directors)
        {
            List<DirectorViewModels> list = new List<DirectorViewModels>();
            foreach (Director director in directors)
            {
                DirectorViewModels directorVM = director;
                list.Add(directorVM);
            }
            return list;
        }

        private static List<Director> ConvertToDirectorList(ICollection<DirectorViewModels> directors)
        {
            List<Director> list = new List<Director>();
            foreach (DirectorViewModels directorVM in directors)
            {
                Director director = directorVM;
                list.Add(director);
            }
            return list;
        }

        private static List<ActorViewModels> ConvertToActorViewModelList(ICollection<Actor> actors)
        {
            List<ActorViewModels> list = new List<ActorViewModels>();
            foreach (Actor actor in actors)
            {
                ActorViewModels actorVM = actor;
                list.Add(actorVM);
            }
            return list;
        }

        private static List<Actor> ConvertToActorList(ICollection<ActorViewModels> actors)
        {
            List<Actor> list = new List<Actor>();
            foreach (ActorViewModels actorVM in actors)
            {
                Actor actor = actorVM;
                list.Add(actor);
            }
            return list;
        }

        private static List<CategoryViewModels> ConvertToCategoryViewModelList(ICollection<Category> categories)
        {
            List<CategoryViewModels> list = new List<CategoryViewModels>();
            foreach (Category category in categories)
            {
                CategoryViewModels categoryVM = category;
                list.Add(categoryVM);
            }
            return list;
        }

        private static List<Category> ConvertToCategoryList(ICollection<CategoryViewModels> categories)
        {
            List<Category> list = new List<Category>();
            foreach (CategoryViewModels categoryVM in categories)
            {
                Category category = categoryVM;
                list.Add(category);
            }
            return list;
        }
    }
}